//
//  DKRegisterPwdView.m
//  DanKU
//
//  Created by 吴斌 on 2023/9/19.
//

#import "DKRegisterPwdView.h"
@interface DKRegisterPwdView()<UITextFieldDelegate>
@property (nonatomic, strong)UITextField * dk_RegisterPwdTextField;
@property (nonatomic, strong)UIButton * dk_RegisterPwdStateBtn;

@property (nonatomic, copy)NSString * loginPwd;
@end
@implementation DKRegisterPwdView
-(instancetype)init{
    if(self == [super init]){
        self.backgroundColor = DK_Hex(@"#F7F7F8");
        self.layer.borderWidth = 0;
        self.layer.borderColor = DK_Hex(@"#4566FF").CGColor;
        self.layer.cornerRadius = 14;
        self.clipsToBounds = YES;
        self.loginPwd = @"";
        [self dk_addSubView];
    }
    return self;
}
- (void)dk_addSubView{
    self.dk_RegisterPwdTextField = [[UITextField alloc] init];
    self.dk_RegisterPwdTextField.textColor = DK_Hex(@"#333333");
    self.dk_RegisterPwdTextField.font = DK_FONT_Regular(15);
    self.dk_RegisterPwdTextField.placeholder = @"Please enter your password";
    self.dk_RegisterPwdTextField.delegate = self;
    [self addSubview:self.dk_RegisterPwdTextField];
    [self.dk_RegisterPwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(20)));
        make.right.equalTo(@(-DK_IsWidthScale(40)));
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(-DK_IsWidthScale(0)));
    }];
    
    self.dk_RegisterPwdStateBtn = [[UIButton alloc] init];
    [self.dk_RegisterPwdStateBtn setImage:DK_GetImage(@"dk_registerPwdHid") forState:UIControlStateNormal];
    [self.dk_RegisterPwdStateBtn setImage:DK_GetImage(@"dk_registerPwdOpen") forState:UIControlStateSelected];
    [self.dk_RegisterPwdStateBtn addTarget:self action:@selector(changePwdState) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.dk_RegisterPwdStateBtn];
    [self.dk_RegisterPwdStateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-DK_IsWidthScale(10)));
        make.top.equalTo(@(DK_IsWidthScale(15)));
        make.bottom.equalTo(@(-DK_IsWidthScale(15)));
        make.width.equalTo(self.dk_RegisterPwdStateBtn.mas_height);
    }];
}
-(void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    self.dk_RegisterPwdTextField.placeholder = _placeholder;
}
- (void)changePwdState{
    self.dk_RegisterPwdStateBtn.selected = !self.dk_RegisterPwdStateBtn.isSelected;
    [self makeShowPwd];
}
- (void)makeShowPwd{
    if(self.dk_RegisterPwdStateBtn.selected){
        self.dk_RegisterPwdTextField.text = self.loginPwd;
    }else{
        NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"[a-zA-Z0-9]" options:0 error:nil];
        self.dk_RegisterPwdTextField.text = [regularExpression stringByReplacingMatchesInString:self.loginPwd options:0 range:NSMakeRange(0, self.loginPwd.length) withTemplate:@"*"];
    }
}
- (NSString *)getLoginPwd{
    return self.loginPwd;
}
#pragma mark UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{//开始编辑
    if(self.StartEditPhoneBlock){
        self.StartEditPhoneBlock();
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    self.loginPwd = [self.loginPwd stringByReplacingCharactersInRange:range withString:string];
    if(self.loginPwd.length > 0){
        self.dk_RegisterPwdStateBtn.hidden = NO;
        self.layer.borderWidth = 0.5;
        if(self.goCheckMsgPwdBlock){
            self.goCheckMsgPwdBlock(false);
        }
    }else{
        self.dk_RegisterPwdStateBtn.hidden = YES;
        self.layer.borderWidth = 0;
        if(self.goCheckMsgPwdBlock){
            self.goCheckMsgPwdBlock(true);
        }
    }
    [self makeShowPwd];
    return NO;
}
@end
